<template>
    <div>
        <navigation-bar>
            <div class="searchContent">
                <custom-button type="add" style="margin-right: 10px" @addClick="addClick"></custom-button>
            </div>
        </navigation-bar>

        <div style="margin-top: 10px">
            <table-index
                :dataSource="dataSource"
                :tableColumns="tableColumns"
                :btnList="btnList"
                :pageSize="pageSize"
                :pageCount="pageCount"
                :pageNo="pageNo"
                @pageChange="pageChange"
                @editRow="editRow"
                @deleteRow="deleteRow"
                selection
                @handleSelectionChange="handleSelectionChange"
                :height="secondTableHeight"
            ></table-index>
        </div>

        <div v-if="dialogShow">
            <custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
                <form-index :formConfig="formConfig" @submit="submit" :type="type" @formInputChange="formInputChange" ref="form">
                    <!--  <div>这里还可以插入其它内容进行拓展</div> -->
                </form-index>
            </custom-dialog>
        </div>
    </div>
</template>

<script>
// import {personOrgForm} from '@/assets/form/managementCenter/systemManage/personOrg'
import {childSysManageForm} from '@/assets/form/managementCenter/systemManage/childSysManage.js'
import customDialog from '@/components/customComponents/customDialog'
import customButton from '@/components/customComponents/customForm/customButton'
import formIndex from '@/components/customComponents/customForm/formIndex'
import tableIndex from '@/components/customComponents/customeTable/iviewTable/singleViewTable'
import navigationBar from '@/components/customComponents/managementCenter/navigationBar'
import {AjaxCommon, printAddress} from '@/libs/apiCommon'
import {CardId} from '@/libs/cardId'
import {PopupCommon} from '@/libs/popupCommon'
import {SessionCommon} from '@/libs/sessionCommon'
import {Utility} from '@/libs/utility'
import {OrgList} from '@/mockData/systemManage.js'
export default {
    components: {
        navigationBar,
        customButton,
        tableIndex,
        customDialog,
        formIndex
    },
    data() {
        return {
            secondTableHeight: document.body.clientHeight - 220,
            selectDeviceList: [],
            unitName: null,
            status: '',
            statusList: Utility.getDictList('status'),
            // table 相关参数  begin
            dataSource: [],
            tableColumns: [
                //              {
                //   "sysGuid": "sample string 1",
                //   "sysName": "sample string 2", // 系统名称
                //   "sysUrl": "sample string 3", // 系统URL
                //   "sys_code": "sample string 4", // 子系统编号
                //   "sysFunction": "sample string 5", // 系统功能描述
                //   "sysSort": 6, // 排序号
                //   "enable": 7 // 是否启用0  int
                // }
                {
                    key: 'name',
                    title: '系统名称'
                },
                {
                    key: 'sysUrl',
                    title: '系统URL'
                },
                {
                    key: 'sys_code',
                    title: '子系统编号'
                },
                {
                    key: 'function',
                    title: '系统功能描述'
                },
                {
                    key: 'sort',
                    title: '排序号'
                },
                {
                    key: 'enable',
                    title: '是否启用',
                    slot: 'enable'
                },
                {
                    title: '操作',
                    slot: 'action',
                    width: 120,
                    align: 'center'
                }
            ],
            btnList: ['edit', 'delete'],
            pageSize: 20,
            pageCount: 100,
            pageNo: 1,
            // table 相关参数  end

            // 弹窗是否显示
            dialogShow: false,
            // 弹窗标题 (新增修改不同)
            dialogTitle: '',

            // form的配置信息
            formConfig: childSysManageForm,
            type: 'add'
        }
    },
    created() {
        // Utility.getDictListByTypeCode('ORG_TYPE').then((res) => {
        //     if (res && res.length > 0) {
        //         const data = res.map((t) => {
        //             return {
        //                 value: t.value,
        //                 label: t.desc
        //             }
        //         })
        //         this.formConfig = personOrgForm(data)
        //     }
        // })
    },
    methods: {
        getHeight() {
            this.secondTableHeight = document.body.clientHeight - 220
        },
        customClick(type) {
            if (this.selectDeviceList.length < 1) {
                PopupCommon.notify('info', '请选择至少一个机构')
                return
            }
            PopupCommon.notify('success', type == 1 ? '启用成功' : '禁用成功')
        },

        handleSelectionChange(d) {
            this.selectDeviceList = d
        },
        formInputChange(e) {
            console.log(e)
        },
        submit(d) {
            let self = this
            d.enable = d.enable ? 1 : 0
            if (d.sysGuid) {
                AjaxCommon('/api/Menu/UpdateSystemInfo', 'post', d).then((res) => {
                    if (res?.data?.Code == 200) {
                        PopupCommon.notify('success', '编辑成功')
                        self.initPage()
                        self.dialogClose()
                    } else {
                        const msg = res?.data?.Message
                        if (msg) {
                            PopupCommon.notify('error', msg)
                        }
                    }
                })
            } else {
                delete d.sysGuid
                // 根据类型调用不同接口
                let url = '/api/Menu/AddSystem'
                AjaxCommon(url, 'post', d).then((res) => {
                    if (res?.data?.Code == 200) {
                        PopupCommon.notify('success', '新增成功')
                        self.initPage()
                        self.dialogClose()
                    } else {
                        const msg = res?.data?.Message
                        if (msg) {
                            PopupCommon.notify('error', msg)
                        }
                    }
                })
            }
        },
        dialogClose() {
            this.dialogShow = false
        },
        searchClick() {
            this.pageNo = 1
            this.initPage()
        },
        resetClick() {
            this.unitName = null
        },
        addClick() {
            let formConfigItem = this.formConfig
            formConfigItem.map((t) => {
                Object.keys(t).map((res) => {
                    if (res == 'fieldName') {
                        t.filedValue = ''
                    }
                })
            })
            this.formConfig = formConfigItem
            this.type = 'add'
            this.dialogTitle = '新增'
            this.dialogShow = true
        },
        // 分页点击事件
        pageChange(p) {
            this.pageNo = p
            this.initPage()
        },
        editRow(d) {
            this.type = 'edit'
            let formConfigItem = this.formConfig
            formConfigItem.map((t) => {
                Object.keys(t).map((res) => {
                    if (t.fieldName == 'sysGuid') {
                        t.filedValue = d['id']
                    } else if (t.fieldName == 'sysName') {
                        t.filedValue = d['name']
                    } else if (t.fieldName == 'sysUrl') {
                        t.filedValue = d['sysUrl']
                    } else if (t.fieldName == 'sys_code') {
                        t.filedValue = d['sys_code']
                    } else if (t.fieldName == 'sysFunction') {
                        t.filedValue = d['function']
                    } else if (t.fieldName == 'sysSort') {
                        t.filedValue = d['sort']
                    } else if (t.fieldName == 'enable') {
                        t.filedValue = d['enable'] == '1'
                    }
                })
            })
            this.formConfig = formConfigItem
            this.dialogTitle = '编辑'
            this.dialogShow = true
        },
        deleteRow(d) {
            const id = d.id
            let self = this
            AjaxCommon(`/api/Menu/DeleteSys/${id}`, 'post', {}).then((res) => {
                console.log(res, 'res...............')
                if (res?.data?.Code == 200) {
                    PopupCommon.notify('success', '删除成功')
                    self.initPage()
                } else {
                    const msg = res?.data?.Message
                    if (msg) {
                        PopupCommon.notify('error', msg)
                    }
                }
            })
        },
        // 初始化事件
        initPage() {
            let self = this
            const params = `?PageSize=${this.pageSize}&PageIndex=${this.pageNo}`
            AjaxCommon(`/api/Menu/GetSysPageData${params}`, 'get', null).then((res) => {
                console.log(res, 'res...........')
                if (res?.data?.Code == 200) {
                    const data = res?.data.Message
                    console.log(data?.rows, 'data?.rows...')
                    self.dataSource = data?.rows
                    self.pageCount = parseInt(data?.total)
                } else {
                    PopupCommon.notify('error', res?.data?.msg)
                }
            })
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.getHeight()
            this.initPage()
        })

        /*窗口自适应，关键代码*/
        window.onresize = () => {
            this.getHeight()
        }
    }
}
</script>

<style></style>



















